<template>
  <div>
    <h2>登录</h2>
    <el-form
      ref="formRef"
      :model="dynamicValidateForm"
      label-width="120px"
      class="demo-dynamic"
    >
      <el-form-item
        prop="loginName"
        label="登录名"
        :rules="[
          {
            required: true,
            message: 'Please input loginName',
            trigger: 'blur',
          },
        ]"
      >
        <el-input v-model="dynamicValidateForm.loginName" />
      </el-form-item>
      <el-form-item
        prop="password"
        label="密码"
        :rules="[
          {
            required: true,
            message: 'Please input password',
            trigger: 'blur',
          },
        ]"
      >
        <el-input type="password" v-model="dynamicValidateForm.password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(formRef as FormInstance)"
          >Submit</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import type { FormInstance } from "element-plus";
const router = useRouter();
const formRef = ref<FormInstance>();
const dynamicValidateForm = reactive<{
  loginName: string;
  password: string;
}>({
  loginName: "",
  password: "",
}); 
const submitForm = (fromEl: FormInstance) => {
  if (!fromEl) return;
  fromEl.validate((valid) => {
    if (valid) {
      console.log(dynamicValidateForm);
      router.push("/");
    }
  });
};
</script>

<style>
</style>